{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b3a6ae7-59e0-4498-814e-a93e1d634cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab39a624-cec1-4102-b23d-640c312b72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "575b8a31-6e1c-4edc-98f8-72c52992797b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd799fac-9c82-4c5a-b308-60691a01a866",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get().trim()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cac8a111-9146-49ea-8989-c6392869eaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a064468c-9c51-4307-ba19-e3ff83c6c4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"NNCB\n",
    "\n",
    "CH -> B\n",
    "HH -> N\n",
    "CB -> H\n",
    "NH -> C\n",
    "HB -> C\n",
    "HC -> B\n",
    "HN -> C\n",
    "NN -> C\n",
    "BH -> H\n",
    "NC -> B\n",
    "NB -> B\n",
    "BN -> B\n",
    "BB -> N\n",
    "BC -> B\n",
    "CC -> N\n",
    "CN -> C\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "878489a4-5e1e-4c04-a209-650185a8de67",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c3e422a6-51fa-4631-82dd-0ef856817384",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun solve(input: String, times: Int): String {\n",
    "    val lines = input.split('\\n').filter { it.length > 0 }\n",
    "    val start = lines[0]\n",
    "    val n = start.length\n",
    "    val mp = mutableMapOf<String, String>()\n",
    "    for (line in lines.slice(1 until lines.size)) {\n",
    "        val (s, t) = line.split(\" -> \")\n",
    "        mp[s] = t\n",
    "    }\n",
    "        \n",
    "    var counter = mutableMapOf<String, Long>()\n",
    "    for (i in 0 until n - 1)\n",
    "        counter[start.slice(i until i + 2)] = counter.getOrDefault(start.slice(i until i + 2), 0) + 1\n",
    "    \n",
    "    repeat(times) {\n",
    "        val newCounter = mutableMapOf<String, Long>()\n",
    "        for ((k, v) in counter) {\n",
    "            val mid = mp[k]!!\n",
    "            val fst = k.slice(0 until 1) + mid\n",
    "            val snd = mid + k.slice(1 until 2)\n",
    "            newCounter[fst] = newCounter.getOrDefault(fst, 0) + v\n",
    "            newCounter[snd] = newCounter.getOrDefault(snd, 0) + v\n",
    "        }\n",
    "        counter = newCounter\n",
    "    }\n",
    "    \n",
    "    val charCounter = LongArray(26)\n",
    "    for ((k, v) in counter) {\n",
    "        charCounter[k[0].code - 'A'.code] += v\n",
    "        charCounter[k[1].code - 'A'.code] += v\n",
    "    }\n",
    "    charCounter[start[0].code - 'A'.code]++\n",
    "    charCounter[start[start.length - 1].code - 'A'.code]++\n",
    "        \n",
    "    val hi = charCounter.maxOrNull()!!\n",
    "    val lo = charCounter.filter { it > 0 }.minOrNull()!!\n",
    "    \n",
    "    return ((hi - lo) / 2).toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "07df67e5-7a24-42e8-a6c4-f555675939df",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    return solve(input, 10)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "eaa273a8-a166-44e0-9023-2449fecbbb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1588"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2ad99e07-5a96-40c4-873c-e72dd7c5f499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3247"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23679ce7-8258-41ae-9813-dff5745b482c",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "528387cf-abb5-4da2-9fd3-9bbf865444a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "   return solve(input, 40)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b12a86b5-8d6f-4a6c-851a-af1d84ae168d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2188189693529"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "62a5e94e-550f-42fe-9597-32e6f87a5b1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4110568157153"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb4614cf-b32f-4f3b-93ce-298387832db7",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
